<?php
/**
 * The form for updating an employee.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-20
 */
require_once "login_info.php";
require_once "employees.php";

loginInfo::update_login();

?>

<html>
	<head>
		<title>Opol - Update Employee</title>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php' ?>
		<br>
<?php
if ($_POST) {
	$db = new database ();
	if (isset($_POST['emp_id'])) {
		// Update the employee's information, and move on.
		$emp = new employee ((int)$_POST['emp_id']);
		$emp->init_from_post ($_POST);
		$result = $emp->update_database_by_id ($db);

		if (!$result) {
			echo "You do not have sufficient permissions to perform this action.<br>\n";
		} else {
			// Offer the option to move on.
			echo "Database Successfully Updated!<br>\n";
		}
		echo "<a href=\"/\">Back to main menu.</a><br>\n";

	} else {

		$emp_id = $_POST['name'];
		if (!$emp_id) {
		} else {
			$emp = new employee ($emp_id);
			$ret = $emp->init($db);
			if (!$ret)
				echo "Error<br>\n";
		}
	}

	$first_name = $emp->first_name;
	$last_name 	= $emp->last_name;
	$machine 	= $emp->machine;
	$shift 		= $emp->shift;

	$machs = $db->query("SELECT id, name FROM Machines");
} else {
	header("location: update_entry.php?type=employee");
}

?>

		<form name="employee" action="" method="post">
			<input type="hidden" name="emp_id" value="<?php print $emp_id;?>">
		<table>
			<tr>
				<td><label>First Name:</label></td>
				<td><input type="text" name="first_name" value="<?php print $first_name;?>"></td>
			</tr>
			<tr>
				<td><label>Last Name:</label></td>
				<td><input type="text" name="last_name" value="<?php print $last_name;?>"></td>
			</tr>
			<tr>
				<td><label>Machine:</label></td>
				<td>
					<select name="machine">
<?php
	// Need to set current selected.
		while ($row = $machs->fetch_assoc()) {
			$id = $row['id']; $name = $row['name'];
			if ($id == $machine)
				print "\t\t\t\t\t\t<option value='$id' selected>$name</option>\n";
			else
				print "\t\t\t\t\t\t<option value='$id'>$name</option>\n";
		}
$db->close ();
?>
					</select>
				</td>
			</tr>
			<tr>
				<td><label>Shift:</label></td>
				<td><input type="text" name="shift" value="<?php print $shift;?>"></td>
			</tr>
			<tr>
				<td><input type="submit" value="Update"></td>
			</tr>
		</form>
	</body>
</html>
